package com.ruoyi.ruoyiserver.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.ruoyiserver.domain.PeDataInformation;
import com.ruoyi.ruoyiserver.mapper.PeDataInformationMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class PeDataInformationListener extends AnalysisEventListener<PeDataInformation> {

    private static final int BATCH_COUNT = 5000;
    private final List<PeDataInformation> cacheList = new ArrayList<>(BATCH_COUNT);
    private final PeDataInformationMapper mapper;

    public PeDataInformationListener(PeDataInformationMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void invoke(PeDataInformation data, AnalysisContext context) {
        data.setId(UUID.randomUUID().toString().replace("-", "").substring(0, 20));
        cacheList.add(data);
        if (cacheList.size() >= BATCH_COUNT) {
            mapper.insertBatchData(cacheList);
            cacheList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (!cacheList.isEmpty()) {
            mapper.insertBatchData(cacheList);
        }
    }
}
